#include "Common.h"
#include "Sock/AppLibSock_Base.hpp"

/**********************************************************
 * Function Name:        AppLibSockNet_SrvCreate
 * Description:          创建服务器socket
 * Parameters[in]:       IN APP_LIB_SOCKET_TYPE_e enType
 *                       IN CHAR *pcIpAddr
 *                       IN USHORT usPort
 *                       INOUT APP_LIB_SOCK_s *pstSrv
 * Parameters[out]:      INOUT APP_LIB_SOCK_s *pstSrv
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2025.11.01
 * Note:
***********************************************************/
ULONG AppLibSockNet_SrvCreate(IN APP_LIB_SOCKET_TYPE_e enType, IN CHAR *pcIpAddr, IN USHORT usPort, INOUT APP_LIB_SOCK_s *pstSrv)
{
    ULONG uRet = STAT_SUCCESS;

    if ( pstSrv == NULL || pcIpAddr == NULL )
    {
        return STAT_ERR_INVAILD_PARAM;
    }

    uRet = AppLibSock_InitInetAddr(enType, pcIpAddr, usPort, &pstSrv->stAddr);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    pstSrv->enType = enType;
    uRet = AppLibSock_SrvCreate(pstSrv, APP_LIB_SOCK_LISTEN_MAX);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    return STAT_SUCCESS;
}

/**********************************************************
 * Function Name:        AppLibSockNet_CliCreate
 * Description:          创建客户端socket
 * Parameters[in]:       IN APP_LIB_SOCKET_TYPE_e enType
 *                       IN CHAR *pcIpAddr
 *                       IN USHORT usPort
 *                       INOUT APP_LIB_SOCK_s *pstCli
 * Parameters[out]:      INOUT APP_LIB_SOCK_s *pstCli
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2025.11.01
 * Note:
***********************************************************/
ULONG AppLibSockNet_CliCreate(IN APP_LIB_SOCKET_TYPE_e enType, IN CHAR * pcIpAddr, IN USHORT usPort, IN APP_LIB_SOCK_s *pstCli)
{
    ULONG  uRet = STAT_SUCCESS;

    if (pstCli == NULL)
    {
        return STAT_ERR_INVAILD_PARAM;
    }

    uRet = AppLibSock_InitInetAddr(enType, pcIpAddr, usPort, &pstCli->stAddr);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    pstCli->enType = enType;
    uRet = AppLibSock_Socket(pstCli);
    if (uRet != STAT_SUCCESS)
    {
        return uRet;
    }

    return STAT_SUCCESS;
}
